|
|
|
|
|
|
|
You should use the preceding line of code sparingly because it really just ignores errors rather than handles them. A better use of Resume is to go to another section of code, as in the following example: |
|
|
|
|
|
|
|
|
Private Sub Form_Load()
On Error GoTo FileOpenError
RetryHere:
Open "C:\NOFILE.TXT" For Input As #1
Line Input #1, sData
Exit Sub
FileOpenError:
Dim sMessage As String
sMessage = "There was a problem opening the file. " & vbCrLf
sMessage = sMessage & "Press Retry to try again, or Cancel to quit."
If MsgBox(sMessage, vbRetryCancel + vbCritical, "Error!") = vbRetry Then
Resume RetryHere
Else
End
End If
End Sub |
|
|
|
|
|
|
|
|
The preceding example displays a message box with Retry and Cancel buttons. If the user clicks Retry, the code resumes executing at the label RetryHere, causing the Open statement to be executed again. |
|
|
|
|
|
|
|
|
Determining the Type of Error |
|
|
|
|
|
|
|
|
After an error has occurred, your code can find out more information about the error in several ways: |
|
|
|
|
|
|
|
|
ErrContains a number that represents the error. |
|
|
|
|
|
|
|
|
ErrorContains a string describing the error. |
|
|
|
|
|
|
|
|
Err ObjectContains error number, description, and additional information. Also used to raise your own custom errors. |
|
|
|
|
|
|
|
|
If you know how to recover from certain errors that may occur, you can use these objects to respond intelligently to a specific error. In the previous example, you received a File Not Found error, which is number 53. You can easily add code in the error handler to take appropriate action (that is, check another file) if the value of Err is equal to 53. |
|
|
|
|
| |
|
|
|
|
When you are writing an error handling routine with a message box, display the error number and description in your message box to make troubleshooting easier. |
|
|
|
|
|
|
|